package com.zxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zxy.common.result.Result;
import com.zxy.pojo.dto.CourseAdDTO;
import com.zxy.pojo.dto.PageDTOCourseDTO;
import com.zxy.pojo.entity.Course;
import com.zxy.pojo.vo.CourseContractResponseVO;
import com.zxy.service.CourseService;
import com.zxy.util.OperLog;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@RequestMapping("/clues")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 分页查询数据
     */
    @OperLog(title = "课程分页查询", businessType = 0, saveParams = true)
    @GetMapping("/course/list")
    public CourseContractResponseVO<Course> pagequery( PageDTOCourseDTO dto){
        return courseService.pagequery(dto);
    }

    /**
     * 添加课程
     */
    @OperLog(title = "新增课程", businessType = 1, saveParams = true, saveResult = true)
    @PostMapping("/course")
    public Result saveOne(@RequestBody CourseAdDTO dto){
        courseService.saveOne(dto);
        return Result.success();
    }

    /**
     * 根据id查询课程
     */
    @OperLog(title = "根据ID查询课程", businessType = 0, saveParams = true)
    @GetMapping("/course/{id}")
    public Result queryOne(@PathVariable Long id){
        Course course = courseService.getById(id);
        Course courseVo = new Course();
        BeanUtils.copyProperties(course, courseVo);
        courseVo.setSubject(String.valueOf(course.getSubjectId()));
        return Result.success(courseVo);
    }

    /**
     * 修改课程
     */
    @OperLog(title = "修改课程信息", businessType = 2, saveParams = true, saveResult = true)
    @PutMapping("/course")
    public Result updateOne(@RequestBody CourseAdDTO dto){
        LambdaUpdateWrapper<Course> wrapper = new LambdaUpdateWrapper<>();
        if(StringUtils.isNotBlank(dto.getApplicablePerson())){
            wrapper.set(Course::getApplicablePerson, dto.getApplicablePerson());
        }
        if(StringUtils.isNotBlank(dto.getInfo())){
            wrapper.set(Course::getInfo, dto.getInfo());
        }
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.set(Course::getName, dto.getName());
        }
        if(dto.getPrice()!=null){
            wrapper.set(Course::getPrice, dto.getPrice());
        }
        if(StringUtils.isNotBlank(dto.getSubject())){
            wrapper.set(Course::getSubjectId, dto.getSubject());
        }
        wrapper.eq(Course::getId, dto.getId());
        boolean update = courseService.update(wrapper);
        return Result.success(update);
    }

    /**
     * 删除数据
     */
    @OperLog(title = "批量删除课程", businessType = 3, saveParams = true, saveResult = true)
    @DeleteMapping("/course/{ids}")
    public Result deleteOne(@PathVariable Collection<Long> ids){
        courseService.removeByIds(ids);
        return Result.success();
    }
}
